/******************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module: appInstance.h
 * Author : Byungil Jeong
 *
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
 *****************************************************************************/
 
#ifndef APPINSTANCE_H_
#define APPINSTANCE_H_

#include "sageDisplay.h"
#include "sage.h"
#include "sageSync.h"
#include "sageCircBuf.h"
#include "sageTcpModule.h"
#include "sageUdpModule.h"
#include "streamInfo.h"
#include "sageStreamer.h"

class appInstance;
class messageInterface;

class streamData {
public:
	appInstance *This;
	pthread_t       thId;
	int senderID;
	int idx;
	int queueID;
	sageMontage montage[2];
	int frontMon;
	bool active, renewMontage, configure, waitingSignal;
	bool updated;
	
	streamData() : active(false), renewMontage(false), frontMon(0), 
					updated(false), configure(false), waitingSignal(false) {}
	sageMontage* getFrontMon() { return &montage[frontMon]; }
	sageMontage* getBackMon() { return &montage[1-frontMon]; }
	int setDepth(float depth);
	int setTileID(int id);
	void swapMontage();
	int clearMontage();
};

class instanceConfig {
public:
	char *masterIP;
	int nodeID;
	int syncPort;
	int nodeNum;
	bool master;
	int bufferMode;
	sageNwConfig nwCfg;
	sageDisplay *displayObj;
	messageInterface *msgInf;
	
	instanceConfig() : nodeID(0), syncPort(0), masterIP(NULL), msgInf(NULL), bufferMode(0) {}
};

class sageStreamer;

class appInstance {
private:
	streamProtocol *nwObj;
	instanceConfig config;
	
	int instID;
	int syncMode;
	int sailNodeNum;
	
	sageCircBuf *blockBuf;
	
	std::vector<int> fsList;
	std::vector<streamData*> streamList;
	sageSyncServer *syncServerObj;	
	sageSyncClient *syncClientObj;
	std::vector<sageStreamer *> streamerList;

	appInExec *fsInfo;
	int waitNodes;
	
	int streamNum;
	int frameNum;
	int masterFrameNum;
	int zValue;
	bool waitingSync;
	bool connectionDone;
	bool streamsUp;

	int oldFrame;
	int reportRate;
	
	double reportTime;
	float slaveBandWidth, accBandWidth;
	unsigned packetLoss;
	float accLoss, slaveLoss;
	double oldTime;
	int  reportCnt;
	
	bool streamStop;
	bool noSync;
	bool unreliable;
	bool displayActive;  // indicate if it receives pixels or not
	
	pthread_mutex_t *condMutex;
	pthread_cond_t  *bufReady;
	pthread_cond_t  *beginWaiting;
	
	static void* syncMasterThread(void* args);
	
	int sendPerformanceInfo();
	int createRcvThread(int senderID, int tileID);
	int initSyncConnection();
	int sendAckToSenders(int flag);
	int sendAckToSender(int senderID, int flag);
	int processSageBlock(sageBlock *block);
	int addStreamer(int fsIdx);
	int addStream(int senderID, int tileID);
	int rcvStreams();
	int instanceReady();
	int shutdownAllStreams();	
	int startPerformanceReport(sageMessage &msg);
	
public:
	appInstance() {}
	~appInstance();
	int init(instanceConfig &conf, int id, streamProtocol *protoObj);
	int fetchSageBlocks();
	bool checkSyncSignal();
	int setDepth(float depth);
	int destroyInstance();
	
	int connectToFSManager();
	int connectToFSManager(char *fsIP, int fsPort);	
	int parseMessage(sageMessage &msg, int fsIdx);
	int startStream(int fsIdx);
	bool isActive();
	int accumulateBandWidth(char *data);
		
	friend class sageBridge;
};

#endif
